//
//  HouseTypeInfoViewModel.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/4/12.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "HouseTypeInfoViewModel.h"
#import "HouseTypeInfoMessageModel.h"
#import "HouseTypeInfoViewController.h"
#import "LayoutCommentsTableViewCellModel.h"
#import "SyncTimer.h"

@implementation HouseTypeInfoViewModel

#pragma mark - Delegate NetworkingBaseManagerAPISuccessCallbackDelegate
- (void)manager:(NetworkingBaseManager *)manager callAPIDidSuccessWithParams:(NSDictionary *)responseParams{
    DDLogInfo(@"查看户型详情网络返回数据：%@",[responseParams description]);
    NSArray *dataArray = [responseParams objectForKey:@"data"];
    
    NSMutableArray *models = [HouseTypeInfoMessageModel mj_objectArrayWithKeyValuesArray:dataArray];
    
    for (HouseTypeInfoMessageModel *model in models) {
        if (model.layoutPicture.count != 0) {
           model.advertisementImageAddress = model.layoutPicture[0]; 
        }
        model.houseArea = [NSString stringWithFormat:@"%@㎡",model.houseArea];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSInteger averagePrice = [[userDefaults objectForKey:@"AveragePrice"] integerValue];
        model.averagePrice = [NSString stringWithFormat:@"%ld",averagePrice];
        model.totalPrice = [NSString stringWithFormat:@"%ld",(NSInteger)(averagePrice*[model.houseArea floatValue]/10000.0)];
        model.magazineId = [[model.magazines objectForKey:@"magazineId"] integerValue];
        model.mediaMagazineId = [[model.mediaMagazineUrl objectForKey:@"magazineId"] integerValue];
        NSMutableString *introductionStr = [NSMutableString string];
        for (NSString *str in model.layoutLocation) {
            [introductionStr appendString:[NSString stringWithFormat:@"%@号楼 ",str]];
        }
        model.houseTypeLocation = introductionStr;
        for (LayoutCommentsTableViewCellModel *cellModel in model.comments) {
            cellModel.date = [SyncTimer timeStampToTimeString:[cellModel.date longLongValue]];
        }
    }
    
    HouseTypeInfoViewController *houseTypeInfoViewController = self.viewObject;
    houseTypeInfoViewController.models = models;
}

#pragma mark - Public
- (void)loadRequest{
    self.manager = [NetworkingManagerFactory generateNormalManager];
    RequestGenerator *requestGenerator = [RequestGenerator sharedInstance];
    self.manager.apiName = [requestGenerator generateCommonApiWithName:[NSString stringWithFormat:@"projects/%ld/layouts/info",[AppContext sharedInstance].projectId]];
    self.manager.params = self.postObject;
    self.manager.requestType = HTTP_GET;
    self.manager.successDelegate = self;
    self.manager.validator = self;
    [self.manager loadRequest];
}

- (void)setUpMJModel{
    [HouseTypeInfoMessageModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{
                 @"houseArea" : @"layoutArea",
                 @"houseTags" : @"layoutTag",
                 @"houseIntroduction" : @"layoutIntroduction",
                 };
    }];
    [LayoutCommentsTableViewCellModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{
                 @"avatarURL" : @"picture",
                 @"startCount" : @"level",
                 @"comment" : @"content",
                 @"positiveCount" : @"goodReviewsCount",
                 @"negativeCount" : @"badReviewsCount"
                 };
    }];
    [HouseTypeInfoMessageModel mj_setupObjectClassInArray:^NSDictionary *{
        return  @{
                  @"comments" : @"LayoutCommentsTableViewCellModel"
                  };
    }];
}


@end
